﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace YXShop.Web.admin.stocks
{
    public partial class In_Stocks_Edit : System.Web.UI.Page
    {
        BLL.Stocks.BarCode bllBarCode = new BLL.Stocks.BarCode();
        BLL.Stocks.OutProduct bllOupProduct = new BLL.Stocks.OutProduct();
        BLL.Stocks.ProductTruck bllProductTruck = new BLL.Stocks.ProductTruck();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitWebControl();
            }
        }

        protected void btnInProduct_Click(object sender, EventArgs e)
        {
            string barcode = txtBarCode.Text.Trim();
             YXShop.Model.Admin.AdminInfo admin = YXShop.Common.AdministrorManager.Get();
            if (barcode != "")
            {
                if (admin != null)
                {
                    Model.Stocks.StockTypeEnum stocktype = (Model.Stocks.StockTypeEnum)int.Parse(this.DropDownListNowStockType.SelectedValue);
                    int stockid = -1;
                    if (stocktype == Model.Stocks.StockTypeEnum.CenterStock)
                    {
                        stockid = 0;
                    }
                    if (stocktype == Model.Stocks.StockTypeEnum.AreaStock)
                    {
                        if (this.hfStoreCenterid0.Value != "")
                        {
                            stockid = int.Parse(this.hfStoreCenterid0.Value);
                        }
                    }
                    if (stocktype == Model.Stocks.StockTypeEnum.ShopStock)
                    {
                        if (this.hfstoreid0.Value != "")
                        {
                            stockid = int.Parse(this.hfstoreid0.Value);
                        }
                    }
                    if (stockid > -1)
                    {
                        int i=InProduct(barcode, stocktype, stockid, admin.AdminId);
                        if (i == 0) //入库成功
                        {
                            this.ltlMsg.Text = "入库成功：条码编号--" + barcode;
                            this.pnlMsg.Visible = true;
                            this.pnlMsg.CssClass = "actionOk";
                            this.txtBarCode.Text = "";
                        }
                    }
                    else
                    {
                        this.ltlMsg.Text = "请选择仓库" + "\n";
                        this.pnlMsg.Visible = true;
                        this.pnlMsg.CssClass = "actionErr";
                    }
                }
                else
                {
                    this.ltlMsg.Text = "用户登录信息出错，请重新登录" + "\n";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                }
            }
            else
            {
                this.ltlMsg.Text = "请输入条形码" + "\n";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
            }

        }

        protected void DropDownListNowStockType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.hfstoreid0.Value = "";
            this.hfStoreCenterid0.Value = "";
            if (DropDownListNowStockType.SelectedItem.Value == "0")//中央仓库
            {
                this.txtCenter0.Visible = true;
                this.txtStoreCenter0.Visible = false;
                this.txtStoreName0.Visible = false;
            }
            if (DropDownListNowStockType.SelectedItem.Value == "1")//区域中心
            {
                this.txtCenter0.Visible = false;
                this.txtStoreCenter0.Visible = true;
                this.txtStoreName0.Visible = false;
            }
            if (DropDownListNowStockType.SelectedItem.Value == "2")//门店
            {
                this.txtCenter0.Visible = false;
                this.txtStoreCenter0.Visible = false;
                this.txtStoreName0.Visible = true;
            }
        }
        #region 验证
        private void InitWebControl()
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            //ChangeHope.WebPage.WebControl.Validate(this.txtBarCode, "条形码不能为空", "isnull", "必填", "该项为必填项");
            this.txtStoreName0.Attributes.Add("readonly", "readonly");
            this.txtStoreName0.Attributes.Add("onclick", "selectFile('Storelist',new Array(" + this.hfstoreid0.ClientID + "," + this.txtStoreName0.ClientID + "),310,450,'" + sp.DummyPaht + "');");
             this.txtStoreCenter0.Attributes.Add("readonly", "readonly");
            this.txtStoreCenter0.Attributes.Add("onclick", "selectFile('Storecenterlist',new Array(" + this.hfStoreCenterid0.ClientID + "," + this.txtStoreCenter0.ClientID + "),310,450,'" + sp.DummyPaht + "');");
            this.Form.Attributes.Add("onsubmit", "return CheckForm()");
        }
        #endregion
        private int InProduct(string barcode, Model.Stocks.StockTypeEnum stockstype, int stockid, int adminid)
        {
            int result = -1;
            #region 判断barcode状态是否为待入库
            bool isState = isStateok(barcode);
            
            #endregion
            if (isState)
            {
                #region 判断barcode的入库设置是否符合
                int matchStock = isMatchStock(barcode,stockstype,stockid);
                #endregion
                if (matchStock == 0)
                {
                    #region  入库
                    #region  更新barcode 状态
                    Model.Stocks.BarCode barcodeModel = bllBarCode.GetModelByBarCode(barcode);
                    barcodeModel.StockTypeID = stockstype;
                    barcodeModel.StockID = stockid;
                    barcodeModel.InTime = DateTime.Now;
                    barcodeModel.StateID = Model.Stocks.ProductStateEnum.Putin;
                    bllBarCode.Update(barcodeModel);
                    #endregion
                    #region  添加到商品跟踪表
                    Model.Stocks.ProductTruck truckModel = new Model.Stocks.ProductTruck();
                    truckModel.BarcodeID = barcodeModel.ID;
                    truckModel.OperationTime = DateTime.Now;
                    truckModel.OperationType = Model.Stocks.ProductStateEnum.Putin;
                    truckModel.StoreType = stockstype;
                    truckModel.StoreID = stockid;
                    truckModel.Adminid = adminid;
                    bllProductTruck.Add(truckModel);
                    #endregion

                    #region  删除outProduct
                    Model.Stocks.OutProduct outProductModel= bllOupProduct.GetModelByBarcodeID(barcodeModel.ID);
                    bllOupProduct.Delete(outProductModel.ID);
                    #endregion
                    #endregion
                    result = 0;
                }
                else //仓库设置不符合出库
                {
                    this.ltlMsg.Text = "仓库设置不是当前商品的待入仓库" + "\n";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                }
            }
            else //状态不符合
            {
                this.ltlMsg.Text = "该商品状态不符合商品入库条件，请检查是否入库 或售出等" + "\n";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
            }

            return result;
        }
        /// <summary>
        /// 判断商品状态 是否可以入库  出库或订购状态时候可以出库
        /// </summary>
        /// <param name="barcode"></param>
        /// <returns></returns>
        private bool isStateok(string barcode)
        {
            bool isMatch = false;
            Model.Stocks.BarCode barcodeModel = bllBarCode.GetModelByBarCode(barcode);
            if (barcodeModel == null)
            {
                isMatch = false;
            }
            else
            {
                if (barcodeModel.StateID == Model.Stocks.ProductStateEnum.Putout || barcodeModel.StateID == Model.Stocks.ProductStateEnum.Order)
                {
                    isMatch = true;
                }
                else
                {
                    isMatch = false;
                }
            }
            return isMatch;
        }
        /// <summary>
        /// 判断是否可以入库   0为可以入库
        /// </summary>
        /// <param name="barcode"></param>
        /// <param name="stockstype"></param>
        /// <param name="stockid"></param>
        /// <returns></returns>
        private int isMatchStock(string barcode, Model.Stocks.StockTypeEnum stockstype, int stockid)
        {
            int result = -1;
            Model.Stocks.BarCode barcodeModel = bllBarCode.GetModelByBarCode(barcode);
            if (barcodeModel == null)
            {
                result = 1;//没有找到该商品
            }
            else
            {
                Model.Stocks.OutProduct outProductModel = bllOupProduct.GetModelByBarcodeID(barcodeModel.ID);
                if (outProductModel == null)
                {
                    result = 2;//该商品不再出库列表中

                }
                else
                {
                    if (!(outProductModel.StockTypeID == stockstype && outProductModel.StockID == stockid))
                    {
                        result = 2;//商品不再所设置的仓库中
                    }
                    else
                    {
                        result = 0;//可以入库
                    }
                }
            }
            return result;
        }
    }
}